#include "Library.h"
#include <fstream>
#include <iostream>

Library::Library() {}

Library::~Library() {
	clearLibrary();
}

void Library::buildLibrary(std::string & path) {
	std::ifstream libraryFile(path.c_str());
	while (libraryFile.good()) {
		std::string currentLine;
		getline(libraryFile, currentLine);
		if (currentLine.length() == 0) {
			continue;
		}
		int delim_loc1 = 0;
		int delim_loc2 = currentLine.find(SEPARATOR);
		std::string artistName = currentLine.substr(delim_loc1, delim_loc2);

		delim_loc1 = delim_loc2 + 1;
		delim_loc2 = currentLine.find(SEPARATOR, delim_loc1);
		std::string albumTitle = currentLine.substr(delim_loc1,
				delim_loc2 - delim_loc1);

		delim_loc1 = delim_loc2 + 1;
		delim_loc2 = currentLine.find(SEPARATOR, delim_loc1);
		std::string songTitle = currentLine.substr(delim_loc1,
				delim_loc2 - delim_loc1);

		delim_loc1 = delim_loc2 + 1;
		delim_loc2 = currentLine.find(SEPARATOR, delim_loc1);
		std::string genre = currentLine.substr(delim_loc1,
			delim_loc2 - delim_loc1);

		delim_loc1 = delim_loc2 + 1;
		delim_loc2 = currentLine.find(SEPARATOR, delim_loc1);
		int year = atoi(currentLine.substr(delim_loc1,
			delim_loc2 - delim_loc1).c_str());

		delim_loc1 = delim_loc2 + 1;
		delim_loc2 = currentLine.find(SEPARATOR, delim_loc1);
		int trackNumber = atoi(currentLine.substr(delim_loc1,
			delim_loc2 - delim_loc1).c_str());

		delim_loc1 = delim_loc2 + 1;
		delim_loc2 = currentLine.find(SEPARATOR, delim_loc1);
		int length = atoi(currentLine.substr(delim_loc1,
			delim_loc2 - delim_loc1).c_str());

		delim_loc1 = delim_loc2 + 1;
		delim_loc2 = currentLine.find(SEPARATOR, delim_loc1);
		int bitrate = atoi(currentLine.substr(delim_loc1,
			delim_loc2 - delim_loc1).c_str());

		delim_loc1 = delim_loc2 + 1;
		std::string path = currentLine.substr(delim_loc1);

		Song * song = new Song(artistName, albumTitle, songTitle, genre, year,
			trackNumber, length, bitrate, path);
		songMap[path] = song;

		Artist * artist = handleArtist(artistName);

		Album * album = handleAlbum(albumTitle, artist);

		album->addSong(song);
	}
	libraryFile.close();
}

void Library::clearLibrary() {
	std::map<std::string, Song *>::iterator songIt;
	for (songIt = songMap.begin(); songIt != songMap.end(); songIt++) {
		delete songIt->second;
	}
	songMap.clear();

	std::map<std::string, Artist *>::iterator artIt;
	for (artIt = artistMap.begin(); artIt != artistMap.end(); artIt++) {
		delete artIt->second;
	}
	artistMap.clear();
}

void Library::printLibrary() {
	std::map<std::string, Song *>::iterator songIt;
	for (songIt = songMap.begin(); songIt != songMap.end(); songIt++) {
		std::cout << (*songIt).second->getSongTitle() << std::endl;
	}
}

Artist * Library::handleArtist(std::string artistName) {
	std::map<std::string, Artist *>::iterator it;
	it = artistMap.find(artistName);
	if (it != artistMap.end()) {
		return it->second;
	}
	Artist * artist = new Artist(artistName);
	artistMap[artistName] = artist;
	return artist;
}

Album * Library::handleAlbum(std::string albumTitle, Artist * artist) {
	std::vector<Album *> * albums = artist->getAlbums();
	for (unsigned int i = 0; i < albums->size(); i++) {
		Album * album = albums->at(i);
		if (album->getAlbumTitle() == albumTitle)
			return album;
	}
	Album * album = new Album(albumTitle);
	albums->push_back(album);
	return album;
}
